package com.beyond.trace.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beyond.trace.domain.TraceBatch;
import com.beyond.trace.domain.storage.Decrease;
import com.beyond.trace.service.storage.IDecreaseService;
import com.beyond.trace.service.storage.IDecreaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.SnowUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 出库信息记录
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/decrease")
@Api(value = "出库信息记录", tags = "出库信息记录")
public class DecreaseController extends BaseController
{
    @Autowired
    private IDecreaseService decreaseService;

    @GetMapping("/getDecreasePage")
    @ApiOperation("分页出库信息记录")
    public TableDataInfo getDecreasePage(Decrease decrease)
    {
        startPage();
        QueryWrapper<Decrease> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(decrease.getDecreaseNo())) {
            queryWrapper.like("decrease_no",decrease.getDecreaseNo());
        }
        if(StringUtils.isNotEmpty(decrease.getDecreaseName())) {
            queryWrapper.like("decrease_name",decrease.getDecreaseName());
        }
        if(StringUtils.isNotEmpty(decrease.getDecreasePerson())) {
            queryWrapper.like("decrease_person",decrease.getDecreasePerson());
        }
        if(StringUtils.isNotNull(decrease.getDecreaseTime())) {
            queryWrapper.eq("decrease_time",decrease.getDecreaseTime());
        }
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        queryWrapper.orderByDesc("create_time");
        List<Decrease> list = decreaseService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping(value = "/getDecreaseById")
    @ApiOperation("通过id查询出库信息记录")
    public AjaxResult getDecreaseById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        if(decreaseService.getById(id) == null) {
            return AjaxResult.error("没有查到信息");
        }
        QueryWrapper<Decrease> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return AjaxResult.success(decreaseService.getOne(queryWrapper));
    }

    @PostMapping(value = "/addDecrease")
    @ApiOperation("添加出库信息记录")
    public AjaxResult addDecrease(@RequestBody @Validated Decrease decrease)
    {
        decrease.setCreateBy(SecurityUtils.getUsername());
        decrease.setUpdateBy(SecurityUtils.getUsername());
        decrease.setCreateTime(new Date());
        decrease.setUpdateTime(new Date());
        decrease.setDepartId(String.valueOf(SecurityUtils.getUserDepartId()));
        if(StringUtils.isEmpty(decrease.getDecreaseNo())) {
            decrease.setDecreaseNo(SnowUtil.getId());
        }
        return AjaxResult.success(decreaseService.save(decrease));
    }

    @PostMapping(value = "/editDecrease")
    @ApiOperation("修改出库信息记录")
    public AjaxResult editDecrease(@RequestBody Decrease decrease)
    {
        if(StringUtils.isEmpty(decrease.getId())) {
            return AjaxResult.error("id不能为空");
        }
        decrease.setUpdateBy(SecurityUtils.getUsername());
        decrease.setUpdateTime(new Date());
        return AjaxResult.success(decreaseService.updateById(decrease));
    }

    @GetMapping(value = "/deleteDecreaseById")
    @ApiOperation("删除出库信息记录")
    public AjaxResult deleteDecreaseById(String id)
    {
        if(StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        List<String> list = Arrays.asList(id.split(","));
        QueryWrapper<Decrease> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",list);
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            queryWrapper.eq("depart_id",SecurityUtils.getUserDepartId());
        }
        return AjaxResult.success(decreaseService.remove(queryWrapper));
    }

    @GetMapping(value = "/getIncreaseProductList")
    @ApiOperation("获取入库的产品列表")
    public AjaxResult getIncreaseProductList()
    {

        return AjaxResult.success(decreaseService.getIncreaseProductList());
    }



}
